(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open XNum
open Logging
open ALCoding
  
module SymbolMap = SymbolTable.SymbolMap
  
let find_file args =
  match args with
    | [ name; file_type; must_exist ] ->
        let n = decode_uc_string "kpse_find_file" name in
        let t = decode_symbol "kpse_find_file" file_type in
        let e = decode_bool "kpse_find_file" must_exist in
        let tt =
          if t = sym_PK then
            `PK
          else if t = sym_TFM then
            `TFM
          else if t = sym_AFM then
            `AFM
          else if t = sym_TeX then
            `TeX
          else if t = sym_Type_1 then
            `Type1
          else if t = sym_TrueType then
            `TrueType
          else if t = sym_OpenType then
            `OpenType
          else if t = sym_Source then
            `Source
          else
            `Source
        in
        let s = KPathSea.find_file (UString.uc_string_to_ascii n) tt e in
          Machine.uc_string_to_char_list (UString.uc_string_of_ascii s)
    | _ ->
        assert false
  
let find_glyph name dpi =
  let n = decode_uc_string "kpse_find_glyph" name in
  let d = Machine.decode_num "kpse_find_glyph" dpi in
  let s =
    KPathSea.find_glyph (UString.uc_string_to_ascii n)
      (int_of_num (round_num d))
  in
    Machine.uc_string_to_char_list (UString.uc_string_of_ascii s)
  

